package com.lanhe.game.terraceEarn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lanhe.common.constant.HttpStatus;
import com.lanhe.common.exception.ServiceException;
import com.lanhe.common.utils.DateUtils;
import com.lanhe.game.sysAppUser.domain.SysAppUser;
import com.lanhe.game.sysAppUser.mapper.SysAppUserMapper;
import com.lanhe.game.terraceEarn.domain.LanheTerraceEarn;
import com.lanhe.game.terraceEarn.mapper.LanheTerraceEarnMapper;
import com.lanhe.game.terraceEarn.service.ILanheTerraceEarnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 媒体平台单Service业务层处理
 * 
 * @author tianliang
 * @date 2024-03-06
 */
@Service
public class LanheTerraceEarnServiceImpl implements ILanheTerraceEarnService
{
    @Autowired
    private LanheTerraceEarnMapper lanheTerraceEarnMapper;
    @Autowired
    private SysAppUserMapper sysAppUserMapper;


    /**
     * 查询媒体平台单
     * 
     * @param terraceId 媒体平台单主键
     * @return 媒体平台单
     */
    @Override
    public LanheTerraceEarn selectLanheTerraceEarnByTerraceId(Long terraceId)
    {
        return lanheTerraceEarnMapper.selectLanheTerraceEarnByTerraceId(terraceId);
    }

    /**
     * 查询媒体平台单列表
     * 
     * @param lanheTerraceEarn 媒体平台单
     * @return 媒体平台单
     */
    @Override
    public List<LanheTerraceEarn> selectLanheTerraceEarnList(LanheTerraceEarn lanheTerraceEarn)
    {
        return lanheTerraceEarnMapper.selectLanheTerraceEarnList(lanheTerraceEarn);
    }

    /**
     * 新增媒体平台单
     * 
     * @param lanheTerraceEarn 媒体平台单
     * @return 结果
     */
    @Override
    public int insertLanheTerraceEarn(LanheTerraceEarn lanheTerraceEarn)
    {
        lanheTerraceEarn.setCreateTime(DateUtils.getNowDate());
        return lanheTerraceEarnMapper.insertLanheTerraceEarn(lanheTerraceEarn);
    }

    /**
     * 修改媒体平台单
     * 
     * @param lanheTerraceEarn 媒体平台单
     * @return 结果
     */
    @Override
    public int updateLanheTerraceEarn(LanheTerraceEarn lanheTerraceEarn)
    {
        return lanheTerraceEarnMapper.updateLanheTerraceEarn(lanheTerraceEarn);
    }

    /**
     * 批量删除媒体平台单
     * 
     * @param terraceIds 需要删除的媒体平台单主键
     * @return 结果
     */
    @Override
    public int deleteLanheTerraceEarnByTerraceIds(Long[] terraceIds)
    {
        return lanheTerraceEarnMapper.deleteLanheTerraceEarnByTerraceIds(terraceIds);
    }

    /**
     * 删除媒体平台单信息
     * 
     * @param terraceId 媒体平台单主键
     * @return 结果
     */
    @Override
    public int deleteLanheTerraceEarnByTerraceId(Long terraceId)
    {
        return lanheTerraceEarnMapper.deleteLanheTerraceEarnByTerraceId(terraceId);
    }

    /**
     * 添加每个用户在不同平不同广告的收益
     * @param lanheTerraceEarn
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertTerraceEarn(LanheTerraceEarn lanheTerraceEarn) {
        // 添加信息记录表
        lanheTerraceEarn.setTerraceId(System.currentTimeMillis());
        lanheTerraceEarn.setCreateTime(new Date());
        lanheTerraceEarnMapper.insertLanheTerraceEarn(lanheTerraceEarn);
        // 修改看广软件的资金
        LambdaQueryWrapper<SysAppUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysAppUser::getAppUserId,lanheTerraceEarn.getAppUserId())
                .eq(SysAppUser::getPackageName,lanheTerraceEarn.getPackageName())
                .eq(SysAppUser::getDelFlag,0);
        SysAppUser sysAppUser = sysAppUserMapper.selectOne(wrapper);
        if (sysAppUser == null){
            throw new ServiceException("用户不存在", HttpStatus.ERROR);
        }
        BigDecimal totalAmount = sysAppUser.getTotalAmount();
        // 如果金额是空的话，就直接添加，否则需要累加
        if (totalAmount == null){
            sysAppUser.setTotalAmount(lanheTerraceEarn.getEarnings());
        }else {
            BigDecimal totalAmountCount = totalAmount.add(lanheTerraceEarn.getEarnings());
            sysAppUser.setTotalAmount(totalAmountCount);
        }
        sysAppUser.setUpdateTime(new Date());
        return sysAppUserMapper.updateById(sysAppUser);
    }
}
